package com.s_recycler;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.widget.Toast;

import com.s7.recycler.layout.api.RefreshLayout;
import com.s7.recycler.layout.listener.OnLoadMoreListener;
import com.s7.recycler.layout.listener.OnRefreshListener;
import com.s_recycler.adapter.RefreshAdapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Random;

public class RefreshActivity extends AppCompatActivity implements OnRefreshListener, OnLoadMoreListener {

    private Random random = new Random();
    private RefreshLayout mRefreshLayout;
    private RecyclerView recycler;
    private RefreshAdapter adapter;
    private LinearLayoutManager layoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refresh);

        mRefreshLayout = findViewById(R.id.refresh);

        recycler = findViewById(R.id.recycler);
        layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        adapter = new RefreshAdapter();

        recycler.setLayoutManager(layoutManager);
        recycler.setAdapter(adapter);

        mRefreshLayout.setEnableAutoLoadMore(true);//开启自动加载功能（非必须）
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.setOnLoadMoreListener(this);

        mRefreshLayout.autoRefresh();

    }

    @Override
    public void onRefresh(@NonNull final RefreshLayout layout) {
        layout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                adapter.refresh(initData(40));
                if (adapter.getItemCount() <= 30) {
                    //还有多的数据
                    layout.finishRefresh();
                } else {
                    //没有更多数据（上拉加载功能将显示没有更多数据）
                    layout.finishRefreshWithNoMoreData();
                }
            }
        }, 2000);
    }

    @Override
    public void onLoadMore(@NonNull final RefreshLayout layout) {
        layout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (random.nextBoolean()) {
                    //如果刷新成功
                    adapter.add(initData(10));
                    if (adapter.getItemCount() <= 30) {
                        //还有多的数据
                        layout.finishLoadMore();
                    } else {
                        //没有更多数据（上拉加载功能将显示没有更多数据）
                        Toast.makeText(getApplication(), "数据全部加载完毕", Toast.LENGTH_SHORT).show();
                        layout.finishLoadMoreWithNoMoreData();//将不会再次触发加载更多事件
                    }
                } else {
                    //刷新失败
                    layout.finishLoadMore(false);
                }
            }
        }, 20000);
    }

    private List<String> initData(int max) {
        List<String> datas = new ArrayList<>();
        for (int i = 0; i < max; i++) {
            if (i < 30) {
                datas.add("测试数据" + (i + 30));
            } else {
                datas.add("测试数据" + i);
            }
        }
       return datas;
    }
}
